package AVL树;

/**
 * @author: fs
 * @date: 2022/6/30 10:13
 * @Description:
 */
public class TreeNode {
    public int val;
    public int bf;
    public TreeNode left;
    public TreeNode right;
    public TreeNode parent;

    public TreeNode(int val){
        this.val = val;
    }
}


class AVLTree{
    public TreeNode root;


    public boolean insert(int val) {
        TreeNode node = new TreeNode(val);
        if(root == null) {
            root = node;
            return true;
        }

        TreeNode parent = null;
        TreeNode cur = root;
        //让node节点的双亲节点(parent)
        while (cur != null) {
            if(cur.val < val) {
                parent = cur;
                cur = cur.right;
            }else if(cur.val == val) {
                return false;
            }else {
                parent = cur;
                cur = cur.left;
            }
        }
        //cur == null
        //判断node,该插入parent的哪个子树.
        if(parent.val < val) {
            parent.right = node;
        }else {
            parent.left = node;
        }
        //
        node.parent = parent;
        cur = node;
        // 平衡因子的修改
        while (parent != null) {
            //先看cur是parent的左还是右  决定平衡因子是++还是--
            if(cur == parent.right) {
                //如果是右树，那么右树高度增加 平衡因子++
                parent.bf++;
            }else {
                //如果是左树，那么左树高度增加 平衡因子--
                parent.bf--;
            }

            //检查当前的平衡因子 是不是绝对值 1  0  -1
            if(parent.bf == 0) {
                //说明已经平衡了
                break;
            }else if(parent.bf == 1 || parent.bf == -1) {
                //继续向上去修改平衡因子
                cur = parent;
                parent = cur.parent;
            }else {
                if(parent.bf == 2) { //右树高-》需要降低右树的高度
                    if(cur.bf == 1) {
                        //左旋
                        rotateL(parent);
                    }else {
                        //cur.bf == -1
                        //右左双旋
                        rotateRL(parent);
                    }
                }else {
                    //parent.bf == -2 左树高-》需要降低左树的高度
                    if(cur.bf == -1) {
                        //右旋
                        rotateR(parent);
                    }else {
                        //cur.bf == 1
                        //左右双旋
                        rotateLR(parent);
                    }
                }
                //上述代码走完就平衡了
                break;
            }
        }
        return true;
    }



    //右旋
    private void rotateR(TreeNode parent) {
        //分步写
        TreeNode subL = parent.left;
        TreeNode subLR = subL.right;

        parent.left = subLR;
        subL.right = parent;

        if(subLR != null){
            subLR.parent = parent;
        }
        TreeNode pParent = parent.parent;
        parent.parent = subL;
        if(root == parent){
            root = subL;
            root.parent = null;
        }else{
            if(pParent.left == parent){
                pParent.left = subL;
            }else{
                pParent.right = subL;
            }
            subL.parent = pParent;
        }
        subL.bf = parent.bf = 0;
    }



    //左单旋
    private void rotateL(TreeNode parent) {
        //获取左旋节点的右子树
        TreeNode subR = parent.right;
        //获取左旋节点的右子树的左子树
        TreeNode subRL = subR.left;
        //左旋节点的右子树变为左旋节点旧右子树的左子树
        parent.right = subRL;
        //旧右子树的左子树变为左旋节点
        subR.left = parent;
        //如果当前旧右子树的左子树不为空,则让其双亲节点指向左旋节点.
        if(subRL != null) {
            subRL.parent = parent;
        }
        //先保存左旋点的双亲节点,好让其指向subR
        TreeNode pParent = parent.parent;
        parent.parent = subR;
        //判断左旋节点是否是根节点
        if(root == parent) {
            root = subR;
            root.parent = null;
        }else {
            //判断左旋节点是双亲节点的哪个节点.
            if(pParent.left == parent) {
                pParent.left = subR;
            }else {
                pParent.right = subR;
            }
            subR.parent = pParent;
        }
        //调整平衡因子
        subR.bf = parent.bf = 0;
    }
    private void rotateLR(TreeNode parent) {
        TreeNode subL = parent.left;
        TreeNode subLR = subL.right;
        int bf = subLR.bf;

        rotateL(parent.left);
        rotateR(parent);

        if(bf == -1) {
            subL.bf = 0;
            subLR.bf = 0;
            parent.bf = 1;
        }else {
            subL.bf = -1;
            subLR.bf = 0;
            parent.bf = 0;
        }
    }
    //右左双旋
    private void rotateRL(TreeNode parent) {
        TreeNode subR = parent.right;
        TreeNode subRL = subR.left;
        int bf = subRL.bf;

        rotateR(parent.right);
        rotateL(parent);

        if(bf == 1) {
            parent.bf = -1;
            subR.bf = 0;
            subRL.bf = 0;
        }else if(bf == -1){
            parent.bf = 0;
            subR.bf = 0;
            subRL.bf = 1;
        }
    }

    private int height(TreeNode root) {
        if(root == null) return 0;
        int leftH = height(root.left);
        int rightH = height(root.right);

        return leftH > rightH ? leftH+1 : rightH+1;
    }

    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;
        int leftH = height(root.left);
        int rightH = height(root.right);

        if(rightH-leftH != root.bf) {
            System.out.println("这个节点："+root.val+" 平衡因子异常");
            return false;
        }

        return Math.abs(leftH-rightH) <= 1
                && isBalanced(root.left)
                && isBalanced(root.right);
    }

    public static void main(String[] args) {
        int[] array =  {16, 3, 7, 11, 9, 26, 18, 14, 15};
        AVLTree avlTree = new AVLTree();
        for (int i = 0; i < array.length; i++) {
            avlTree.insert(array[i]);
        }
        System.out.println(avlTree.isBalanced(avlTree.root));
    }
}
